package cn.wsn2018.tools.ctrl;

import cn.wsn2018.tools.common.CommonValue;
import cn.wsn2018.tools.model.LoginUser;
import cn.wsn2018.tools.model.MySessionContext;
import cn.wsn2018.tools.model.User;
import cn.wsn2018.tools.service.ProtocolTestService;
import cn.wsn2018.tools.util.ServletUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by NZX on 2018/12/24.
 */
@RestController
@RequestMapping("/protocol")
/**
 * 处理测试请求的控制类
 */
public class ProtocolTestController {

    @Autowired
    private ProtocolTestService protocolTestService;

    @CrossOrigin
    @RequestMapping(value = "/model/protocolName",method = RequestMethod.GET)
    /**
     * 切换模式
     */
    public void changeModel(@RequestParam("protocolName")String protocolName,
                            HttpServletRequest request,HttpServletResponse response) throws Exception {
        ServletUtil.getInstance().requestOperating(request);
        String respJson = protocolTestService.changeModel(protocolName);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }


    @CrossOrigin
    @RequestMapping(value = "/authNegoTest",method = RequestMethod.GET)
    /**
     * 认证协商测试
     */
    public void authNegoTest(@RequestParam("id")String id,
                             @RequestParam("testNum")String testNum,
                             @RequestParam("attack")String attack,
                             @RequestParam("token")String token, HttpServletRequest request, HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myUsername = session.getAttribute("username").toString();
        //ServletUtil.updateUserMapVal(Thread.currentThread().getId(),new LoginUser(myUsername, CommonValue.TESTMODULE));
        String respJson = protocolTestService.dealAuthNegoTest(id,testNum,attack,myUsername);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }

    @CrossOrigin
    @RequestMapping(value = "/encryptNegoTest",method = RequestMethod.GET)
    /**
     * 加密协商测试
     */
    public void encryptNegoTest(@RequestParam("id")String id,
                             @RequestParam("testNum")String testNum,
                             @RequestParam("token")String token, HttpServletRequest request, HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myUsername = session.getAttribute("username").toString();
        //ServletUtil.updateUserMapVal(Thread.currentThread().getId(),new LoginUser(myUsername, CommonValue.TESTMODULE));
        String respJson = protocolTestService.dealEncryptNegoTest(id,testNum,myUsername);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }

    @CrossOrigin
    @RequestMapping(value = "/encryptTest",method = RequestMethod.GET)
    /**
     * 加密测试
     */
    public void encryptTest(@RequestParam("id")String id,
                             @RequestParam("testNum")String testNum,
                             @RequestParam("attack")String attack,
                             @RequestParam("token")String token, HttpServletRequest request, HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myUsername = session.getAttribute("username").toString();
        //ServletUtil.updateUserMapVal(Thread.currentThread().getId(),new LoginUser(myUsername, CommonValue.TESTMODULE));
        String respJson = protocolTestService.dealEncryptTest(id,testNum,attack,myUsername);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }

    @CrossOrigin
    @RequestMapping(value = "/exceptionTest",method = RequestMethod.GET)
    /**
     * 异常测试
     */
    public void exceptionTest(@RequestParam("id")String id,
                             @RequestParam("testNum")String testNum,
                             @RequestParam("attack")String attack,
                             @RequestParam("token")String token, HttpServletRequest request, HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myUsername = session.getAttribute("username").toString();
        //ServletUtil.updateUserMapVal(Thread.currentThread().getId(),new LoginUser(myUsername, CommonValue.TESTMODULE));
        String respJson = protocolTestService.dealExceptionTest(id,testNum,attack,myUsername);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }

    @CrossOrigin
    @RequestMapping(value = "/resendTest",method = RequestMethod.GET)
    /**
     * 重传测试
     */
    public void resendTest(@RequestParam("id")String id,
                             @RequestParam("testNum")String testNum,
                             @RequestParam("attack")String attack,
                             @RequestParam("token")String token, HttpServletRequest request, HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myUsername = session.getAttribute("username").toString();
        //ServletUtil.updateUserMapVal(Thread.currentThread().getId(),new LoginUser(myUsername, CommonValue.TESTMODULE));
        String respJson = protocolTestService.dealResendTest(id,testNum,attack,myUsername);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }

    @CrossOrigin
    @RequestMapping(value = "/integrityTest",method = RequestMethod.GET)
    /**
     * 完整性测试
     */
    public void integrityTest(@RequestParam("id")String id,
                             @RequestParam("testNum")String testNum,
                             @RequestParam("attack")String attack,
                             @RequestParam("token")String token, HttpServletRequest request, HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myUsername = session.getAttribute("username").toString();
       // ServletUtil.updateUserMapVal(Thread.currentThread().getId(),new LoginUser(myUsername, CommonValue.TESTMODULE));
        String respJson = protocolTestService.dealIntegrityTest(id,testNum,attack,myUsername);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }

    @CrossOrigin
    @RequestMapping(value = "/protocolTest",method = RequestMethod.GET)
    /**
     * 测试
     */
    public void testProtocol(@RequestParam("id")String id,
                             @RequestParam("attacks")String attacks,
                             @RequestParam("token")String token, HttpServletRequest request, HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myUsername = session.getAttribute("username").toString();
        //ServletUtil.updateUserMapVal(Thread.currentThread().getId(),new LoginUser(myUsername, CommonValue.TESTMODULE));
        String respJson = protocolTestService.dealProtocolTest(id,attacks,myUsername);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }

    /*@CrossOrigin
    @RequestMapping(value = "/testReports",method = RequestMethod.POST)
    *//**
     * 保存测试报告
     *//*
    public void saveTestReport(@RequestParam("token")String token,
                               @RequestBody TestReport report,HttpServletRequest request,HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myUsername = session.getAttribute("username").toString();
        String respJson = protocolTestService.dealTestReportSave(report,myUsername);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }*/

    @CrossOrigin
    @RequestMapping(value = "/testReports/id",method = RequestMethod.GET)
    /**
     * 查询报告详情
     */
    public void findTestReport(@RequestParam("id")String id,HttpServletRequest request,HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        String respJson = protocolTestService.dealTestReportQuery(id);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }

    @CrossOrigin
    @RequestMapping(value = "/testReports/username",method = RequestMethod.GET)
    /**
     * 查询所有报告
     */
    public void listTestReports(@RequestParam("username")String username,
                                @RequestParam("pageNum") String pageNum,
                                @RequestParam("pageSize")String pageSize,HttpServletRequest request,HttpServletResponse response)throws Exception{
       // ServletUtil.updateUserMapVal(Thread.currentThread().getId(),new LoginUser(username, CommonValue.TESTMODULE));
        ServletUtil.getInstance().requestOperating(request);
        String respJson = protocolTestService.dealTestReportsQuery(username,pageNum,pageSize);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }

    @CrossOrigin
    @RequestMapping(value = "/testReports/id",method = RequestMethod.DELETE)
    /**
     * 删除报告
     */
    public void deleteTestReport(@RequestParam("id")String id,
                                @RequestParam("token")String token,HttpServletRequest request,HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myUsername = session.getAttribute("username").toString();
        //ServletUtil.updateUserMapVal(Thread.currentThread().getId(),new LoginUser(myUsername, CommonValue.TESTMODULE));
        String respJson = protocolTestService.dealTestReportDelete(id,myUsername);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }

    @CrossOrigin
    @RequestMapping(value = "/testReports",method = RequestMethod.DELETE)
    /**
     * 批量删除报告
     */
    public void deleteTestReports(@RequestParam("idList")List<String> idList,
                                 @RequestParam("token")String token,HttpServletRequest request,HttpServletResponse response)throws Exception{
        ServletUtil.getInstance().requestOperating(request);
        MySessionContext myc= MySessionContext.getInstance();
        HttpSession session = myc.getSession(token);
        String myUsername = session.getAttribute("username").toString();
        //ServletUtil.updateUserMapVal(Thread.currentThread().getId(),new LoginUser(myUsername, CommonValue.TESTMODULE));
        String respJson = protocolTestService.dealTestReportsDelete(idList,myUsername);
        ServletUtil.getInstance().responseOperating(response, respJson);
    }

    @CrossOrigin
    @RequestMapping(value = "/preMessage",method = RequestMethod.GET)
    /**
     * 预信息
     * @param designId
     * @param token
     * @param request
     * @param response
     * @throws Exception
     */
    public void sendPreMessage(@RequestParam("designId")String designId,
                               @RequestParam("token")String token,HttpServletRequest request,HttpServletResponse response) throws Exception {
        ServletUtil.getInstance().requestOperating(request);
        String respJson = protocolTestService.dealTestPreInfo(designId);
        ServletUtil.getInstance().responseOperating(response,respJson);
    }

}
